clear all;
load('random.mat','comm_loc','random_sample');

nrandom=size(random_sample,1);
nmkt=size(comm_loc,1);
nyear=size(comm_loc,2);
nbranch=sum(comm_loc);
nbranch=[0,nbranch(2:end)-nbranch(1)];

year_open_data(1:nmkt,1)=999;
for i=1:nmkt
    temp=find(comm_loc(i,:),1);
    if ~isempty(temp)
        year_open_data(i)=temp;
    end
end


year_diff(1:nrandom,1)=nan;

for i_random=1:nrandom
    year_open_model(1:nmkt,1)=999;
    year_open_model(year_open_data==1)=1;
    for i_t=2:nyear
        year_open_model(random_sample(i_random,nbranch(i_t-1)+1:nbranch(i_t)))=i_t;
    end

    gap=abs(year_open_data-year_open_model);
    gap(gap>10)=10;
    year_diff(i_random)=sum(gap)/1022;
end

mean(year_diff);
save('result_random_assignment.mat','year_diff')